<template>
  <div>
    <table border="1" width="700" style="border-collapse: collapse">
      <caption>
        购物车
      </caption>
      <thead>
        <tr>
          <th><input type="checkbox"
          v-model="isAll"
        
           /> <span>全选</span></th>
          <th>名称</th>
          <th>价格</th>
          <th>数量</th>
          <th>总价</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody v-for="(item, index) in goodList" :key="index">
        <tr>
          <th><input type="checkbox" 
           :checked="item.checked"
           @change="change(item.name)"
          /> <span></span></th>
          <th>{{ item.name }}</th>
          <th>{{ item.price }}</th>
          <th>
            <button @click="addnum(item.name)">+</button>
            {{ item.num }}
            <button @click="delnum(item.name)">-</button>
          </th>
          <th>{{ item.price * item.num }}</th>
          <th><button @click="dellist(item.name)">删除</button></th>
        </tr>
      </tbody>
      <tfoot>
        <tr>
          <td>合计:{{sumScore}}</td>
          <td colspan="5"></td>
        </tr>
      </tfoot>
    </table>
  </div>
</template>

<script>
export default {
  props: {
    goodList: {
      type: Array,
      required: true,
    },
  },
   
  methods: {
    dellist(name) {
      this.$emit('delgood', name)
    },
    addnum(name) {
      this.$emit('addloogsum', name)
      // console.log('1');
    },
    delnum(name) {
      this.$emit('delgoodsun', name)
    },
     change(name) {
      this.$emit('change', name)
    },
    addcheckedd() {
      this.$emit('changeAll')
    }
  },
  //计算
  computed:{
    sumScore() {
      return this.goodList.reduce((sum,item) => 
      sum + (item.price * item.num),0)
    },
     isAll: {
      get() {
        return this.goodList.every((v) => v.checked)
      },
       set(val) {
        this.$emit('changeAll', val)
      },
      
    },
  },
 
  
}
</script>

<style></style>
